//
//  ViewController.m
//  ChatApp
//
//  Created by DJL on 15/12/20.
//  Copyright © 2015年 DJL. All rights reserved.
//

#import "ViewController.h"
#import "EaseMob.h"
#import "AppDelegate.h"
#import "RegisteredViewController.h"
@interface ViewController ()<UITextFieldDelegate, UIAlertViewDelegate,IChatManagerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *logoImage;
@property (weak, nonatomic) IBOutlet UITextField *userName;

@property (weak, nonatomic) IBOutlet UITextField *password;

@property (weak, nonatomic) IBOutlet UIButton *login;
@property (weak, nonatomic) IBOutlet UIButton *forgotPassword;
@property (weak, nonatomic) IBOutlet UIButton *AutomaticLogin;
@property (weak, nonatomic) IBOutlet UIButton *registered;
@property (nonatomic, assign)BOOL  isOn;
@end

@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}
//登陆方法
- (IBAction)loginAction:(id)sender {
    /* 通过单例获取通知中心对象 */
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"yonghuming" object:self.userName.text userInfo:nil];
    //异步登录方法
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.userName.text password:self.password.text completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error && loginInfo) {
            NSLog(@"登陆成功");
            //IChatManagerDelegate回调方法 接口调用
            
            [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.userName .text password:self.password .text];
            
            if (self.isOn == NO) {
                // 设置自动登录
                [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:NO];
            } else {
                //设置自动登录
                [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
            }
            
            
            
         }else {
            NSLog(@"denglushibai::%@",error);
            UIAlertController *alert =  [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"%@",error] preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                
            }];
            /* alert添加action对象 */
            [alert addAction:actionConfirm];
            /* 推出 */
            [self presentViewController:alert animated:YES completion:^{
                
                
            }];
            
        }
        
        
        
        
        
    } onQueue:nil];
    

    
}

//忘记密码
- (IBAction)forgetPassword:(id)sender {
}

//自动登录
- (IBAction)automaticLogin:(id)sender {
    self.isOn = !self.isOn;
    if (self.isOn == YES) {
        [sender setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        
        // 设置自动登录
        [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
        
    }else {
        
        [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
      
        // 设置自动登录
        [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:NO];
        
    }
}

//注册账号
- (IBAction)registeredAction:(id)sender {
    
    RegisteredViewController *registere = [[RegisteredViewController alloc] init];
    
    [self presentViewController:registere animated:YES completion:^{
        
        
    }];
    
}
- (void)viewWillAppear:(BOOL)animated {
    
  
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"提示"];
    self.userName. text =passWord;
    self.password .text = @"";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];

    BOOL isAutoLogin = [[EaseMob sharedInstance].chatManager isAutoLoginEnabled];
    if (isAutoLogin) {
        self.isOn = YES;
        [self.AutomaticLogin setTitleColor:[UIColor greenColor] forState:UIControlStateNormal ];
        
    }else {
        [self.AutomaticLogin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
        self.isOn = NO;
    }
}
//完成登陆回调
- (void)didLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error {
    
    NSLog(@"回调完成完成登陆回调0.0");
    UIApplication *application = [UIApplication sharedApplication];
    
    AppDelegate *app = application.delegate;
    app.window.rootViewController = app.tb;
    
    
}
//将要自动登录
- (void)willAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error {
    
    NSLog(@"开始自动登录");
    
}

//自动登录完成的回调
- (void)didAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
